#include "io/vsMedia.h"

#include <QFileInfo>
#include "io/vsFileOps.h"
#include "io/libAV.h"


const QString& statusString(vsTypes::fileStatus status)
{
    static QString _stopped      = QObject::tr("Stopped");
    static QString _checking     = QObject::tr("Checking");
    static QString _converting   = QObject::tr("Converting");
    static QString _downloading  = QObject::tr("Downloading");
    static QString _seeding      = QObject::tr("Seeding");

    switch(status)
    {
    case vsTypes::checking:     return _checking;
    case vsTypes::converting:   return _converting;
    case vsTypes::downloading:  return _downloading;
    case vsTypes::seeding:      return _seeding;
    default:                    return _stopped;
    }
}


vsMediaFile::vsMediaFile(const QString &path, QObject *parent)
    : QObject(parent), path(path), size(0), duration(0)
{
    //
}

vsMediaFile::~vsMediaFile()
{
    //
}

void vsMediaFile::onInfoUpdated()
{
    emit mediaInfoUpdated();
}

//--------------------------------------------------------

Q_GLOBAL_STATIC(vsMedia, vsmedia_instance)

vsMedia* vsMedia::instance()
{
    return vsmedia_instance();
}


vsMedia::vsMedia(QObject *parent)
    : QObject(parent)
{
    formats["ogg"]  = vsMediaTypeSettings("ogg",  "audio/ogg");
    formats["mp3"]  = vsMediaTypeSettings("mp3",  "audio/mpeg");
    formats["aac"]  = vsMediaTypeSettings("aac",  "audio/aac");
    formats["m4a"]  = vsMediaTypeSettings("m4a",  "audio/mp4a-latm");

    formats["flac"] = vsMediaTypeSettings("flac", "audio/x-flac");
    formats["tta"]  = vsMediaTypeSettings("tta",  "audio/x-tta");
    formats["wav"]  = vsMediaTypeSettings("wav",  "audio/wav");

    formats["webm"] = vsMediaTypeSettings("webm", "video/webm");
    formats["mp4"]  = vsMediaTypeSettings("mp4",  "video/mp4");
    formats["flv"]  = vsMediaTypeSettings("flv",  "video/x-flv");
    formats["mkv"]  = vsMediaTypeSettings("mkv",  "video/x-matroska");

    libAVctrl* libAVc = new libAVctrl(this);
    connect(this, SIGNAL(getMediaInfo(vsMediaFile*)), libAVc, SLOT(getInfo(vsMediaFile*)));
}

vsMedia::~vsMedia()
{
    //
}

//--------------------------------------------

bool vsMedia::supports(const QString &suffix, vsMediaTypeSettings *info)
{
    bool result = formats.contains(suffix);

    if (result && info != 0)
        *info = formats[suffix];

    return result;
}


QString vsMedia::mime(const QString &suffix)
{
    QString result;

    if (!suffix.isEmpty() && formats.contains(suffix))
        result = formats[suffix].mime;

    return result;
}

QMap<QString, QString> vsMedia::supportedTypes()
{
    QMap<QString, QString> result;

    foreach (const QString &mime, formats.keys())
        result[formats[mime].ext] = mime;

    return result;
}


bool vsMedia::requestMediaInfo(vsMediaFile* f)
{
    bool result = false;

    if (!(f == 0 || f->path.isEmpty()))
    {
        QFileInfo fi(f->path);

        if (fi.exists() && fi.isFile() && fi.isReadable())
        {
            emit getMediaInfo(f);
            result = true;
        }
    }

    return result;
}
